package com.lgj.web.sys.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.result.ResultBody;
import com.lgj.web.sys.model.SysResource;
import com.lgj.web.sys.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/resource")
public class SysResourceController {

    @Autowired
    private SysResourceService resourceService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody create(@RequestBody SysResource SysResource) {
        int count = resourceService.create(SysResource);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(@PathVariable Long id,
                               @RequestBody SysResource SysResource) {
        int count = resourceService.update(id, SysResource);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<SysResource> getItem(@PathVariable Long id) {
        SysResource SysResource = resourceService.getItem(id);
        return ResultBody.success(SysResource);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(@PathVariable Long id) {
        int count = resourceService.delete(id);
        if (count > 0) {
            return ResultBody.success(count);
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<PageInfo> list(@RequestParam(required = false) Long categoryId,
                                     @RequestParam(required = false) String nameKeyword,
                                     @RequestParam(required = false) String urlKeyword,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return ResultBody.success(resourceService.list(categoryId,nameKeyword, urlKeyword, pageSize, pageNum));
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysResource>> listAll() {
        List<SysResource> resourceList = resourceService.listAll();
        return ResultBody.success(resourceList);
    }
}
